﻿using System;
using System.IO;
using Hongsh.Pay.Client.Proxy.Http;
using MapleLeaf.Pay;
using MapleLeaf.Pay.Pay;
using MapleLeaf.Pay.Trade;

namespace Hongsh.Pay.Client.Proxy.Handlers {
    // Token: 0x0200004D RID: 77
    public static class DFTProxy {
        // Token: 0x06000273 RID: 627 RVA: 0x0000395D File Offset: 0x00001B5D
        public static void AddDFTProxyProxyHandler(this HttpServer svr) {
            svr.RouterList.Add("GET/jrc2ckgu/pay/channelService/store/T0081/channels/paycode", new HttpContextActionDelegate(PayCode));
            svr.RouterList.Add("POST/jrc2ckgu/pay/payService/store/T0081/submit", new HttpContextActionDelegate(Process));
        }

        // Token: 0x06000274 RID: 628 RVA: 0x0000D6FC File Offset: 0x0000B8FC
        private static void PayCode(HttpContext ctx) {
            TradePayway tradePayway = TradeAuthPaymentRequest.AuthcodeGetTradePayway(ctx.Request.UrlForm["pay_code"]);
            object obj = null;
            TradePayway tradePayway2 = tradePayway;
            TradePayway tradePayway3 = tradePayway2;
            if (tradePayway3 != TradePayway.微信) {
                if (tradePayway3 - TradePayway.支付宝 <= 1) {
                    obj = new {
                        code = "aliPay",
                        name = "支付宝",
                        logoUrl = "http://cloudmember.oss-cn-qingdao.aliyuncs.com/platform/pay/%E6%94%AF%E4%BB%98%E5%AE%9D.png"
                    };
                }
            }
            else {
                obj = new {
                    code = "weiXin",
                    name = "微信",
                    logoUrl = "http://cloudmember.oss-cn-qingdao.aliyuncs.com/platform/pay/%E5%BE%AE%E4%BF%A1.png"
                };
            }
            ctx.Result = new HttpJsonResult(new {
                echoCode = "0",
                channels = new object[]
                {
                    obj
                }
            });
        }

        // Token: 0x06000275 RID: 629 RVA: 0x0000D780 File Offset: 0x0000B980
        private static void Process(HttpContext ctx) {
            DFTPayResult dftpayResult = new DFTPayResult {
                echoCode = "0",
                echoMessage = "OK"
            };
            try {
                DFTPayRequest dftpayRequest = new StreamReader(ctx.Request).ReadToEnd().JsonDecode<DFTPayRequest>();
                HongshPayTrade hongshPayTrade;
                if (ctx.Request.Url.AbsolutePath.EndsWith("submit", StringComparison.OrdinalIgnoreCase)) {
                    hongshPayTrade = ctx.Execute(new TradeAuthPaymentRequest {
                        Authcode = dftpayRequest.payCode,
                        Amount = (int)Math.Floor(dftpayRequest.amount * 100m),
                        ClientTradeId = dftpayRequest.orderNumber
                    });
                    dftpayResult.methodName = "b2cpay";
                }
                else {
                    if (!ctx.Request.Url.AbsolutePath.EndsWith("queryStatus", StringComparison.OrdinalIgnoreCase)) {
                        throw new Exception("不支持的请求方法！");
                    }
                    hongshPayTrade = ctx.Execute(new TradePaymentQueryRequest {
                        ClientTradeId = dftpayRequest.orderNumber
                    });
                    dftpayResult.methodName = "payquery";
                }
                dftpayResult.PayNo = hongshPayTrade.TradeId;
                dftpayResult.amount = (int)Math.Floor(hongshPayTrade.Amount / 100m);
                dftpayResult.orderNumber = dftpayRequest.orderNumber;
                if (hongshPayTrade.State == TradeState.失败) {
                    throw new Exception(hongshPayTrade.StateDescript);
                }
                if (hongshPayTrade.State == TradeState.成功) {
                    dftpayResult.echoMessage = "支付成功！";
                    dftpayResult.payAccount = hongshPayTrade.Payer;
                    dftpayResult.settlementAmount = dftpayResult.payAmount = Math.Round(hongshPayTrade.ActualAmount / 100m, 2);
                    dftpayResult.PayTime = hongshPayTrade.UpdateTime.ToString();
                }
            }
            catch (Exception ex) {
                dftpayResult.echoCode = "1";
                dftpayResult.echoMessage = ex.Message;
            }
            ctx.Result = new HttpJsonResult(dftpayResult);
        }

        // Token: 0x0200004E RID: 78
        public class DFTGoods {
            // Token: 0x1700010D RID: 269
            // (get) Token: 0x06000276 RID: 630 RVA: 0x00003997 File Offset: 0x00001B97
            // (set) Token: 0x06000277 RID: 631 RVA: 0x0000399F File Offset: 0x00001B9F
            public decimal amount { get; set; }

            // Token: 0x1700010E RID: 270
            // (get) Token: 0x06000278 RID: 632 RVA: 0x000039A8 File Offset: 0x00001BA8
            // (set) Token: 0x06000279 RID: 633 RVA: 0x000039B0 File Offset: 0x00001BB0
            public int productId { get; set; }

            // Token: 0x1700010F RID: 271
            // (get) Token: 0x0600027A RID: 634 RVA: 0x000039B9 File Offset: 0x00001BB9
            // (set) Token: 0x0600027B RID: 635 RVA: 0x000039C1 File Offset: 0x00001BC1
            public int productName { get; set; }

            // Token: 0x17000110 RID: 272
            // (get) Token: 0x0600027C RID: 636 RVA: 0x000039CA File Offset: 0x00001BCA
            // (set) Token: 0x0600027D RID: 637 RVA: 0x000039D2 File Offset: 0x00001BD2
            public int quantity { get; set; }

            // Token: 0x0600027E RID: 638 RVA: 0x0000286D File Offset: 0x00000A6D
            public DFTGoods() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }

        // Token: 0x0200004F RID: 79
        public class DFTPayRequest {
            // Token: 0x17000111 RID: 273
            // (get) Token: 0x0600027F RID: 639 RVA: 0x000039DB File Offset: 0x00001BDB
            // (set) Token: 0x06000280 RID: 640 RVA: 0x000039E3 File Offset: 0x00001BE3
            public decimal amount { get; set; }

            // Token: 0x17000112 RID: 274
            // (get) Token: 0x06000281 RID: 641 RVA: 0x000039EC File Offset: 0x00001BEC
            // (set) Token: 0x06000282 RID: 642 RVA: 0x000039F4 File Offset: 0x00001BF4
            public string @operator { get; set; }

            // Token: 0x17000113 RID: 275
            // (get) Token: 0x06000283 RID: 643 RVA: 0x000039FD File Offset: 0x00001BFD
            // (set) Token: 0x06000284 RID: 644 RVA: 0x00003A05 File Offset: 0x00001C05
            public string orderNumber { get; set; }

            // Token: 0x17000114 RID: 276
            // (get) Token: 0x06000285 RID: 645 RVA: 0x00003A0E File Offset: 0x00001C0E
            // (set) Token: 0x06000286 RID: 646 RVA: 0x00003A16 File Offset: 0x00001C16
            public string payCode { get; set; }

            // Token: 0x17000115 RID: 277
            // (get) Token: 0x06000287 RID: 647 RVA: 0x00003A1F File Offset: 0x00001C1F
            // (set) Token: 0x06000288 RID: 648 RVA: 0x00003A27 File Offset: 0x00001C27
            public string posNo { get; set; }

            // Token: 0x17000116 RID: 278
            // (get) Token: 0x06000289 RID: 649 RVA: 0x00003A30 File Offset: 0x00001C30
            // (set) Token: 0x0600028A RID: 650 RVA: 0x00003A38 File Offset: 0x00001C38
            public string uuid { get; set; }

            // Token: 0x17000117 RID: 279
            // (get) Token: 0x0600028B RID: 651 RVA: 0x00003A41 File Offset: 0x00001C41
            // (set) Token: 0x0600028C RID: 652 RVA: 0x00003A49 File Offset: 0x00001C49
            public DFTGoods[] prodcuts { get; set; }

            // Token: 0x0600028D RID: 653 RVA: 0x0000286D File Offset: 0x00000A6D
            public DFTPayRequest() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }

        // Token: 0x02000050 RID: 80
        public class DFTPayResult {
            // Token: 0x17000118 RID: 280
            // (get) Token: 0x0600028E RID: 654 RVA: 0x00003A52 File Offset: 0x00001C52
            // (set) Token: 0x0600028F RID: 655 RVA: 0x00003A5A File Offset: 0x00001C5A
            public string echoCode { get; set; }

            // Token: 0x17000119 RID: 281
            // (get) Token: 0x06000290 RID: 656 RVA: 0x00003A63 File Offset: 0x00001C63
            // (set) Token: 0x06000291 RID: 657 RVA: 0x00003A6B File Offset: 0x00001C6B
            public string echoMessage { get; set; }

            // Token: 0x1700011A RID: 282
            // (get) Token: 0x06000292 RID: 658 RVA: 0x00003A74 File Offset: 0x00001C74
            // (set) Token: 0x06000293 RID: 659 RVA: 0x00003A7C File Offset: 0x00001C7C
            public decimal amount { get; set; }

            // Token: 0x1700011B RID: 283
            // (get) Token: 0x06000294 RID: 660 RVA: 0x00003A85 File Offset: 0x00001C85
            // (set) Token: 0x06000295 RID: 661 RVA: 0x00003A8D File Offset: 0x00001C8D
            public string methodName { get; set; }

            // Token: 0x1700011C RID: 284
            // (get) Token: 0x06000296 RID: 662 RVA: 0x00003A96 File Offset: 0x00001C96
            // (set) Token: 0x06000297 RID: 663 RVA: 0x00003A9E File Offset: 0x00001C9E
            public string orderNumber { get; set; }

            // Token: 0x1700011D RID: 285
            // (get) Token: 0x06000298 RID: 664 RVA: 0x00003AA7 File Offset: 0x00001CA7
            // (set) Token: 0x06000299 RID: 665 RVA: 0x00003AAF File Offset: 0x00001CAF
            public string payAccount { get; set; }

            // Token: 0x1700011E RID: 286
            // (get) Token: 0x0600029A RID: 666 RVA: 0x00003AB8 File Offset: 0x00001CB8
            public string payAccountId {
                get {
                    return payAccount;
                }
            }

            // Token: 0x1700011F RID: 287
            // (get) Token: 0x0600029B RID: 667 RVA: 0x00003AC0 File Offset: 0x00001CC0
            // (set) Token: 0x0600029C RID: 668 RVA: 0x00003AC8 File Offset: 0x00001CC8
            public decimal payAmount { get; set; }

            // Token: 0x17000120 RID: 288
            // (get) Token: 0x0600029D RID: 669 RVA: 0x00003AD1 File Offset: 0x00001CD1
            // (set) Token: 0x0600029E RID: 670 RVA: 0x00003AD9 File Offset: 0x00001CD9
            public string PayNo { get; set; }

            // Token: 0x17000121 RID: 289
            // (get) Token: 0x0600029F RID: 671 RVA: 0x00003AE2 File Offset: 0x00001CE2
            // (set) Token: 0x060002A0 RID: 672 RVA: 0x00003AEA File Offset: 0x00001CEA
            public string PayTime { get; set; }

            // Token: 0x17000122 RID: 290
            // (get) Token: 0x060002A1 RID: 673 RVA: 0x00003AF3 File Offset: 0x00001CF3
            public string PayTunnel { get; }

            // Token: 0x17000123 RID: 291
            // (get) Token: 0x060002A2 RID: 674 RVA: 0x00003AFB File Offset: 0x00001CFB
            public string channelPayNo {
                get {
                    return PayNo;
                }
            }

            // Token: 0x17000124 RID: 292
            // (get) Token: 0x060002A3 RID: 675 RVA: 0x00003B03 File Offset: 0x00001D03
            // (set) Token: 0x060002A4 RID: 676 RVA: 0x00003B0B File Offset: 0x00001D0B
            public decimal settlementAmount { get; set; }

            // Token: 0x060002A5 RID: 677 RVA: 0x00003B14 File Offset: 0x00001D14
            public DFTPayResult() {
                // Class15.cwcZ3SWztHroi();
                PayTunnel = "direct";
                // base..ctor();
            }
        }
    }
}
